package com.lngsyz.ywtb;

import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import com.lngsyz.ywtb.entity.LoginInfo;
import com.lngsyz.ywtb.util.jwt.TokenUtil;

/**
 * 用于绑定@LoginInfo的方法参数解析器
 *
 */

@Service
public class LoginInfoArgumentResolver implements HandlerMethodArgumentResolver {

	@Override
	public boolean supportsParameter(MethodParameter methodParameter) {
		return methodParameter.getParameterType().equals(LoginInfo.class);
	}

	@Override
	public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer,
			NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
		Object obj = nativeWebRequest.getAttribute(TokenUtil.LOGIN_USER, 0);
		return obj;
	}
}
